package com.xr.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xr.constant.MessageConstant;
import com.xr.constant.RedisConstant;
import com.xr.exception.CustomException;
import com.xr.pojo.CheckGroup;
import com.xr.pojo.SetMeal;
import com.xr.query.QueryPageBean;
import com.xr.result.PageResult;
import com.xr.result.Result;
import com.xr.service.SetMealService;
import com.xr.utils.QiNiuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("setMeal")
public class SetMealController {
    //远程订阅服务 注解注入
    @Reference
    private SetMealService setMealService;

    @Autowired
    private JedisPool jedisPool; //操作Redis服务

    //删除
    @RequestMapping("/delete")
    public Result delete(Integer id) throws CustomException {
        setMealService.delete(id);
        return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
    }


    //    修改
    @RequestMapping("/update")
    public Result update(@RequestBody SetMeal setMeal, Integer[] checkgroupIds) {
        try {
            setMealService.update(setMeal, checkgroupIds);
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    //  根据套餐id找出检查组id
    @RequestMapping("/findCheckGroupIdsByMealId")
    public Result findCheckGroupIdsByMealId(Integer id) {
        try {
            List<Integer> ids = setMealService.selectGroupIdByMealId(id);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_SETMEAL_SUCCESS);
    }

    //    根据ID查询
    @RequestMapping("/queryById")
    public Result queryById(Integer id) {
        try {
            SetMeal setMeal = setMealService.queryById(id);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setMeal);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_SETMEALLIST_FAIL);
    }

    //    图片上传
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) {
        System.out.println("img+" + imgFile);
        //原始文件名
        String originalFilename = imgFile.getOriginalFilename();
        //获取后缀
        int i = originalFilename.lastIndexOf(".");
        String substring = originalFilename.substring(i);//.jpg
        //使用UUID生成新的文件名 重复会覆盖
        String fileName = UUID.randomUUID().toString() + substring;
        //通过七牛云工具类字节文件上传
        try {
            System.out.println("join");
            QiNiuUtils.uploadViaByte(imgFile.getBytes(), fileName);
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES, fileName);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
        return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, fileName);
    }

    //    条件查询+分页
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult page = setMealService.findPage(queryPageBean);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, page);
    }

    //    添加
    @RequestMapping("/add")
    public Result add(@RequestBody SetMeal setMeal, Integer[] checkgroupIds) {
        try {
            System.out.println("cg:" + checkgroupIds);
            setMealService.add(setMeal, checkgroupIds);
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
    }

}
